'''
Created on Oct 3, 2009

@author: hbasria
'''
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic

from django.utils.translation import ugettext as _

class Packager(models.Model):
    name = models.CharField(max_length=100)
    email = models.CharField(max_length=100)
    
    def __unicode__(self):
        return self.name
    
class Archive(models.Model):
    type = models.CharField(max_length=10)
    sha1sum = models.CharField(max_length=100)
    value = models.TextField()

class Package(models.Model):
    name = models.CharField(max_length=100)
    icon = models.CharField(max_length=100, blank=True, null=True)
    part_of = models.CharField(max_length=100)

    def __unicode__(self):
        return self.name
    
    def get_dependencies(self):
        name_list = []
        
        for dependency in self.dependency_package.all():
            name_list.append(dependency.dependency.name)           
            
        return ", ".join(name_list)
    
class Source(models.Model):    
    name = models.CharField(max_length=100)
    home_page = models.CharField(max_length=100)
    packager = models.ManyToManyField(Packager, blank=True)
    icon = models.CharField(max_length=100, blank=True, null=True)
    part_of = models.CharField(max_length=100)    
    uri = models.TextField()
    
    archive = models.ForeignKey(Archive, blank=True, null=True)
    package = models.ForeignKey(Package, blank=True, null=True)
    
    def __unicode__(self):
        return self.name
    
    def get_dependencies(self):
        name_list = []
        
        for dependency in self.dependency_source.all():
            name_list.append(dependency.dependency.name)           
            
        return ", ".join(name_list)
    
class License(models.Model):
    value = models.CharField(max_length=100)
    
    def __unicode__(self):
        return self.value
    
class SourceLicense(models.Model):
    source = models.ForeignKey(Source)
    license = models.ForeignKey(License)
        
class PackageLicense(models.Model):
    package = models.ForeignKey(Package)
    license = models.ForeignKey(License)

class Isa(models.Model):
    value = models.CharField(max_length=100)
        
    def __unicode__(self):
        return self.value
    
class SourceIsa(models.Model):
    source = models.ForeignKey(Source)
    isa = models.ForeignKey(Isa)
        
class PackageIsa(models.Model):
    package = models.ForeignKey(Package)
    isa = models.ForeignKey(Isa)

class Summary(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

    lang = models.CharField(max_length=2)
    value = models.TextField()
    
    def __unicode__(self):
        return '(%s) %s' % (self.lang, self.value)
    
class Description(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

    lang = models.CharField(max_length=2)
    value = models.TextField()
    
    def __unicode__(self):
        return '(%s) %s' % (self.lang, self.value)
    

    
class Dependency(models.Model):
    version_from = models.CharField(max_length=100, blank=True, null=True)
    version_to = models.CharField(max_length=100, blank=True, null=True)
    version = models.CharField(max_length=100, blank=True, null=True)
    release_from = models.CharField(max_length=100, blank=True, null=True)
    release_to = models.CharField(max_length=100, blank=True, null=True)
    release = models.CharField(max_length=100, blank=True, null=True)
    
    source = models.ForeignKey(Source, related_name='dependency_source', blank=True, null=True)
    package = models.ForeignKey(Package, related_name='dependency_package', blank=True, null=True)    
    dependency = models.ForeignKey(Package, related_name='dependency_dependency')
    
class Conflict(models.Model):
    version_from = models.CharField(max_length=100, blank=True, null=True)
    version_to = models.CharField(max_length=100, blank=True, null=True)
    version = models.CharField(max_length=100, blank=True, null=True)
    release_from = models.CharField(max_length=100, blank=True, null=True)
    release_to = models.CharField(max_length=100, blank=True, null=True)
    release = models.CharField(max_length=100, blank=True, null=True)
    
    package = models.ForeignKey(Package, related_name='conflict_package')  
    conflict = models.ForeignKey(Package, related_name='conflict_conflict')
    
class Replace(models.Model):
    version_from = models.CharField(max_length=100, blank=True, null=True)
    version_to = models.CharField(max_length=100, blank=True, null=True)
    version = models.CharField(max_length=100, blank=True, null=True)
    release_from = models.CharField(max_length=100, blank=True, null=True)
    release_to = models.CharField(max_length=100, blank=True, null=True)
    release = models.CharField(max_length=100, blank=True, null=True)
    
    package = models.ForeignKey(Package, related_name='replace_package')  
    replace = models.ForeignKey(Package, related_name='replace_replace')   
    

class File(models.Model):
    package = models.ForeignKey(Package)
    file_type = models.CharField(max_length=100)
    permanent = models.BooleanField()
    path = models.TextField()
    
class AdditionalFile(models.Model):
    package = models.ForeignKey(Package)
    target = models.CharField(max_length=100)
    permission = models.CharField(max_length=100)
    owner = models.CharField(max_length=100)
    group = models.CharField(max_length=100)
    value = models.TextField()
    
class Patch(models.Model):
    source = models.ForeignKey(Source)
    compression_type = models.CharField(max_length=100, blank=True, null=True)
    level = models.CharField(max_length=100, blank=True, null=True)
    target = models.CharField(max_length=100, blank=True, null=True)
    value = models.TextField()

    

    

